package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.vocab.service.VocabDyadRemoveService;

@Controller
public class VocabDyadRemoveController {

	@Autowired
	private VocabDyadRemoveService vocabDyadRemoveService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_remove_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("removeFlagKey", "-1");
			pageParam.put("typeKey", "o+oa");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String typeKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "typeKey");
		String arrayKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "arrayKey");
		String removeFlagKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "removeFlagKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return vocabDyadRemoveService.list(start, typeKey, arrayKey, removeFlagKey, exactSearchFlag,countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_remove_add.do")
	public ReturnData add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String array = req.getParameter("array").trim();
		String partPattern = req.getParameter("partPattern").trim();

		// 分词及属性格式校验
		if (!array.startsWith(",") || !partPattern.startsWith(",") || !array.endsWith(",")
				|| !partPattern.endsWith(",")) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc("参数分隔符错误！");
			return ReturnUtil.fail(rc);
		}
		if(array.split(",").length != partPattern.split(",").length) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc("分词与属性的个数不同！");
			return ReturnUtil.fail(rc);
		}
		// 主动添加的分词只能是o+oa或o+s
		String type = "o+oa";
		boolean flag = false;
		for(String property:partPattern.substring(1).split(",")) {
			if(!property.equals("o") && !property.equals("oa") && !property.equals("s")) {
				flag = true;
				break;
			}
			if(property.equals("s")) {
				type = "o+s";
			}
		}
		if(flag) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc("属性中有错误！");
			return ReturnUtil.fail(rc);
		}
		return vocabDyadRemoveService.add(array, partPattern, type);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_remove_check_page.do")
	public ReturnData checkPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		Map<String, Object> map = req.getParameterMap();
		String[] ids = (String[]) map.get("id");
		String[] flags = (String[]) map.get("flag");
		String[] resultArray = (String[]) map.get("resultArray");
		String[] resultPattern = (String[]) map.get("resultPattern");
		
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String type = (String)pageParam.get("typeKey");
		
		return vocabDyadRemoveService.checkPage(type, ids, flags, resultArray, resultPattern, user);
	}
}
